package it.polimi.traveldream.web.beans;

import it.polimi.traveldream.ejb.management.EscursioneDTO;
import it.polimi.traveldream.ejb.management.HotelDTO;
import it.polimi.traveldream.ejb.management.PPrenotatiDTO;
import it.polimi.traveldream.ejb.management.PackDTO;
import it.polimi.traveldream.ejb.management.PackMng;
import it.polimi.traveldream.ejb.management.PrenotazMng;
import it.polimi.traveldream.ejb.management.UserMgr;
import it.polimi.traveldream.ejb.management.VoloDTO;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "friendBean")
@SessionScoped
public class FriendBean {

	
	private String ids;
	private int id;
	
	private PackDTO selectedPack;
	
	private PPrenotatiDTO prenotazione = new PPrenotatiDTO();
	
	@EJB
	private UserMgr userMgr;
	
	
	@EJB
	private PrenotazMng prenotazMgr;
	
	@EJB
	private PackMng packMgr;
	
	@PostConstruct
	public void init() {
		ids =  FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
		id = Integer.parseInt(ids);
		selectedPack = packMgr.searchById(id);
	}

	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
		selectedPack = packMgr.searchById(id);
		
	}

	
	
	
	public PackDTO getSelectedPack() {  
        return selectedPack;  
    }
	
	public void setSelectPack(PackDTO selectedPack) {
		this.selectedPack = selectedPack;
	}
	
	public String prenota() {
		return "/pagine_clienti/prenotaAmico?faces-redirect=true&includeViewParams=true";
	}
	
	public String confermaPrenotaz(){
    	convertToPrenotaz();
    	prenotazMgr.execQuery(selectedPack);
    	prenotazMgr.save(prenotazione);
       return "/pagine_clienti/prenotOk?faces-redirect=true";
    }
    
    private void convertToPrenotaz() {
    	prenotazione.setIdescursione(selectedPack.getEscursione());
    	prenotazione.setIdhotel(selectedPack.getHotel());
    	prenotazione.setDataPartenza(selectedPack.getDataPartenza());
    	prenotazione.setDataRitorno(selectedPack.getDataArrivo());
    	prenotazione.setLuogo(selectedPack.getLuogo());
    	prenotazione.setNomePacchetto(selectedPack.getNome());
    	prenotazione.setIdvoloandata(selectedPack.getVoloAndata());
    	prenotazione.setIdvoloritorno(selectedPack.getVoloRitorno());
    	prenotazione.setMailutente(userMgr.getUserDTO().getEmail());
    }
}    